﻿//********************************************************************************//
//创建日期：<创建日期，2017-7-10>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：来函登记管理
//********************************************************************************//
layui.use(['form', 'jquery', 'element', 'laydate'], function () {
    var form = layui.form()
    , layer = layui.layer
    , $ = layui.jquery
    , element = layui.element()
    , laydate = layui.laydate;

    /*initial load start*/
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var rqstr = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
    var index = layer.load(1);
    initcontrol();
    load();
    layer.close(index);


    /*initial load end*/
    function aa() {
        initcontrol();
        settimeout(layer.close(index), 500);
    };
    /*initial*/
    function initcontrol() {
        //自来水需要加载左侧站点与管理块(当前未启用)
        var wtype = getWType();
        loadControls(wtype);
        setUI(wtype);
    }

   

    function loadControls(wtype){
        if (wtype == -1) {
            //加载站点
            $("#eZhanDian option").remove();
            var rs = getZDList("all", "_");
            var fubbstrs = "";
            var strtemp = "";
            var childstr = "";
            for (var i = 0; i < rs.length; i++) {
                strtemp = "<div class='layui-colla-item' vid='" + rs[i]["Value"] + "' layui-show><h2 class='layui-colla-title'><b style='font-size:bold'>"
                    + rs[i]["Text"] + "</b></h2><div class='layui-colla-content'><table class='layui-table' style='width:100%;' ></table></div></div>";
                fubbstrs = fubbstrs + strtemp;
            }
            $("#divList").append(fubbstrs);
            //加载管理块
            rs = getGLKList("all", "_");
            $("#divList .layui-colla-item").each(function () {
                childstr = "";
                for (var i = 0; i < rs.length; i++) {
                    if (rs[i]["ParentId"] == $(this).attr("vid")) {
                        strtemp = "<tr class='datarow' style='cursor:pointer' vid='" + rs[i]["Value"] + "'><td style='padding-left:20px;'><b>" + rs[i]["Text"] + "</b></td></tr>";
                        childstr = childstr + strtemp;
                    }
                }
                $(this).find("table").append(childstr);
            });
            element.init();      
        }
        //加载来函类型
        $("#cType option:gt(0)").remove();
        if (wtype == 0) {
            $("#cType").append("<option value='1'>月计划调整</option>");
            $("#cType").append("<option value='2'>年计划调整</option>");
            $("#cType").append("<option value='3'>计划转移</option>");
            $("#cType").append("<option value='5'>临时用水(原基建)</option>");
            $("#cType").append("<option value='6'>计划变更</option>");
            $("#cType").append("<option value='7'>退款</option>");
            $("#cType").append("<option value='8'>分户</option>");
            $("#cType").append("<option value='9'>并户</option>");
            $("#cType").append("<option value='10'>改户</option>");
            $("#cType").append("<option value='11'>销户</option>");
            $("#cType").append("<option value='12'>销表</option>");
            $("#cType").append("<option value='13'>表计因素注销</option>");
            $("#cType").append("<option value='14'>计划指标注销</option>");
        }
        else if(wtype==1){
            $("#cType").append("<option value='22'>销户</option>");
            $("#cType").append("<option value='35'>销取水口</option>");
            $("#cType").append("<option value='24'>销表</option>");
            $("#cType").append("<option value='36'>取水口过户</option>");
            $("#cType").append("<option value='28'>分户</option>");
            $("#cType").append("<option value='21'>并户</option>");
        }

        else if(wtype==2){
            $("#cType").append("<option value='22'>销户</option>");
            $("#cType").append("<option value='23'>销井</option>");
            $("#cType").append("<option value='24'>销表</option>");
            $("#cType").append("<option value='25'>注销井恢复</option>");
            $("#cType").append("<option value='26'>井过户</option>");
            $("#cType").append("<option value='27'>表过户</option>");
            $("#cType").append("<option value='28'>分户</option>");
            $("#cType").append("<option value='29'>深井类型修改</option>");
            $("#cType").append("<option value='30'>井性质修改</option>");
            $("#cType").append("<option value='31'>退款</option>"); 
            $("#cType").append("<option value='32'>开采计划调整</option>");
            $("#cType").append("<option value='33'>采灌计划调整</option>");
            $("#cType").append("<option value='34'>加价水量调整</option>");
            $("#cType").append("<option value='21'>并户</option>");
        }
        form.render("select");
    }
    function setUI(wtype) {
        if (wtype == 0) {
            //$("#btnSpeed").show();
            //$("#divL").show();
            //$("#divR").css("width", "86.5%");
        }
        else {
            $("[xt='0']").hide();
            //$("#btnSpeed").hide();
            $("#divL").hide();
            $("#divR").css("width", "100%");
            $("#ctXKSLH").hide();
        }
    }

    function deleteLaihan(id) {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/LH/LaiHan.ashx",
            data: {
                "op": "DJSC",
                "lhbh": id
            },
            dataType: "json",
            success: function (rs) {
                load();
            },
            error: function (error) {
                load();
            }
        });
    }

    function load() {
        var glk = $("#divL").is(":hidden") ? "" : $("#divList .layui-colla-item[selected='selected']").attr("vid");
        var lhlx = $("#cType").val();
        var lhbh = $("#cLHBH").val();
        var yhh = $("#cYHH").val();
        var xkslh = $("#cXKSLH").val();
        var rss = "";
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/LH/LaiHan.ashx",
            data: {
                "op": "DJCX",
                "glk": glk,
                "lhlx": lhlx=="0"?"":lhlx,
                "lhbh": lhbh,
                "yhh": yhh,
                "xkslh": xkslh
            },
            dataType: "json",
            success: function (rs) {
                rss = rs;
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        $("#ContentList1").find(".datarow").remove();
        if (rss.length > 0) {
            chunk(rss, list1Create, bindListOperate);  //  setTimeout(loadColor, 1000) --(追加)
            
        }
        else {
            //$("#ls1Count").text("0");
            //$("#ls1JESum").text("0.00");
        }
        
    }
    
    function bindListOperate() {
        setSelectedInfo("","","");
        $("#ContentList1 .datarow").click(function () {
            var selected=$(this).attr("selected");
            //移除之前选中项背景色及标识
            $("#ContentList1 .datarow[selected='selected']").css("background-color", "");
            $("#ContentList1 .datarow[selected='selected']").removeAttr("selected");
            setSelectedInfo("","","");
            $("#objId").attr("vid", "");//来函编号
            $("#objId").attr("vid2", "");//ID
            $("#objId").attr("vid3", "");//来函类型
            //如果点击在已选择项上,无需继续选中
            if (selected != "selected") {
                //给当前项加标识
                $(this).attr("selected", "selected");
                $(this).css("background-color", "yellowgreen");
                setSelectedInfo($(this).attr("vid"),$(this).attr("vid2"),$(this).attr("vid3"));
            }
        });
    }

    //判断来函存在期效 加载颜色
   function loadColor() {
        $("#ContentList1").find('tbody').each(function (index, item) {
            $(this).find('tr').each(function () {
                var tdArr = $(this).children();
                //td里的内容
                var lhlx = tdArr.eq(5).text(); //来函类型
                var diffDay = tdArr.eq(1).text();//剩余天数
                if(lhlx=='月计划调整'||lhlx=='年计划调整'||lhlx=='计划变更'||lhlx=='计划转移'||lhlx=='临时用水(原基建)')
                {
                   
                    //按相差天数设置颜色
                    if (diffDay <= 2 && diffDay >= 0)
                    {
                        $(this).css("color", " 	#FF8C00");
                    } else if (diffDay < 0)
                    {
                        $(this).css("color", "red");
                    } else {
                        $(this).css("color", "black");
                    }
                    $(tdArr.eq(1)).css("color", "#3CB371");
                }
            
            });
        });
    }
    function list1Create(rs, i) {
        $("#ContentList1").append(createItemStr1(rs));
        //加载一行 剩余天数改变颜色
        loadColor(); 
        //$("#ls1Count").text($("#ContentList1 .datarow").length);
        //var jeSum = 0;
        //$("#ContentList1 .datarow").each(function () {
        //    jeSum = jeSum + parseFloat($(this).find("td").eq(5).text());
        //});
        //$("#ls1JESum").text(jeSum.toFixed(2));
    }
    var orders = 0;
    function createItemStr1(obj) {
        var str = $("#ItemStr1 tr").prop("outerHTML");
        var itemstr, lhbh, id, ilhlx, xkslh, tyspbm, lhlx, yhh, yhm, bz, ym, ywy, lhrq, djr, djrq, sprq, spjg, blzt, days;
        orders++;
        lhbh = obj["S_LAIHANBH"];
        id = obj["ID"];
        ilhlx=obj["I_LAIHANLX"];
        xkslh = obj["S_XUKEBH"];
        tyspbm = obj["S_TONGYIBM"];
        lhlx = obj["S_LAIHANLX"];
        yhh = obj["S_YONGHUBH"];
        yhm = obj["S_YONGHUM"];
        bz=obj["S_BEIZHU"];
        ym = obj["S_YM"];
        ywy = obj["S_YWY"];
        lhrq = obj["D_LAIHANRQ"]["DateString"];
        djr = obj["S_DENGJIR"];
        djrq = obj["D_SHURURQ"]["DateString"];
        sprq = obj["D_SHENPIRQ"]["DateString"];
        spjg = obj["S_SHENPIJG"];
        blrq = obj["S_LIUCHENGZT"];
        days = obj["Days"];
        //orders = obj["orders"];
        itemstr = str.replace(new RegExp("objLAIHANBH", "gm"), lhbh)
                        .replace(new RegExp("objID", "gm"), id)
                        .replace(new RegExp("objILAIHANLX", "gm"), ilhlx)
                        .replace(new RegExp("objXUKESLH", "gm"), xkslh)
                        .replace(new RegExp("objTONGYISPBM", "gm"), tyspbm)
                        .replace(new RegExp("objLAIHANLX", "gm"), lhlx)
                        .replace(new RegExp("objYHH", "gm"), yhh)
                        .replace(new RegExp("objYHM", "gm"), yhm)
                        .replace(new RegExp("objBEIZHU", "gm"), bz)
                        .replace(new RegExp("objYM", "gm"), ym)
                        .replace(new RegExp("objYWY", "gm"), ywy)
                        //.replace(new RegExp("objLAIHANRQ", "gm"), lhrq)
                        .replace(new RegExp("objDENGJIRR", "gm"), djr)
                        .replace(new RegExp("objDENGJIRQ", "gm"), djrq)
                        .replace(new RegExp("objBANLIZT", "gm"), blrq)
                        .replace(new RegExp("objOrders", "gm"), orders)
                        .replace(new RegExp("objDAYS", "gm"), days)
        ;
        return itemstr;
    }
    function setSelectedInfo(lhbh,id,lhlx)
    {
        $("#objId").attr("vid",lhbh );//来函编号
        $("#objId").attr("vid2",id );//ID
        $("#objId").attr("vid3", lhlx);//来函类型
    }

    /*end initial*/

    /*event*/
    $("#btnAdd").click(function () {
        var lhlx = $("#cType").val();
        var lhbh = $("#cLHBH").val();
        if (lhlx == "0")
        {
            layer.alert("请选择要添加的来函类型");
            return;
        }
        openAddOrEdit(0, lhlx, "", "");
        orders = 0;
    });
    $("#btnEdit").click(function () {        
        var lhbh = $("#objId").attr("vid");
        var id = $("#objId").attr("vid2");
        var lhlx = $("#objId").attr("vid3");
        if (lhbh =="") {
            layer.alert("请选择要修改的来函");
            return;
        }
        openAddOrEdit(1, lhlx, id, lhbh);
        orders = 0;
    });
    $("#btnDelete").click(function () {
        var lhbh = $("#objId").attr("vid");
        var id = $("#objId").attr("vid2");
        var lhlx = $("#objId").attr("vid3");
        if (lhbh == "") {
            layer.alert("请选择要删除的来函");
            return;
        }
        deleteLaihan(lhbh);
        orders = 0;
    });
    function openAddOrEdit(type, lhlx, id,lhbh) {
        var url = "";
        var lhlx = lhlx;
        var width = "1000";
        var height = "580";
        switch (lhlx) {
            case "1": url = "/Pages/YongShui/13.LaihanGL/YUEDUTZDJ.aspx";
                height = "560";
                break;
            case "2": url = "/Pages/YongShui/13.LaihanGL/NIANDUTZDJ.aspx";
                break;
            case "3": url = "/Pages/YongShui/13.LaihanGL/JiHuaZYDJ.aspx";
                //height = "650";
                break;
            case "4": url = "/Pages/YongShui/13.LaihanGL/YUEDUTZDJ.aspx";
                break;
            case "5": url = "/Pages/YongShui/13.LaihanGL/JiJianTZDJ.aspx";
                break;
            case "6": url = "/Pages/YongShui/13.LaihanGL/JiHuaBGDJ.aspx";
                height = "560";
                break;
            case "7": url = "/Pages/YongShui/13.LaihanGL/TuiKuanDJ.aspx";
                //width = "900";
                //height = "620";
                break;
            case "8": url = //"/Pages/YongShui/13.LaihanGL/FenHuDJ.aspx";
                url = "/Pages/YongShui/13.LaihanGL/MulFenHuDJ.aspx";
                break;
            case "9": url = "/Pages/YongShui/13.LaihanGL/BingHuDJ.aspx";
                width = "900";
                //height = "620";
                break;
            case "10": url = "/Pages/YongShui/13.LaihanGL/GaiHuDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "11": url = "/Pages/YongShui/13.LaihanGL/XiaoHuDJ.aspx";
                width = "900";
                break;
            case "12": url = "/Pages/YongShui/13.LaihanGL/XiaoBiaoDJ.aspx";
                break;
            case "13": url = "/Pages/YongShui/13.LaihanGL/BiaoJiYSZXDJ.aspx";
                height = "560";
                break;
            case "14": url = "/Pages/YongShui/13.LaihanGL/JiHuaZBZXDJ.aspx";
                break;
            case "21": url = "/Pages/QuShui/13.LaihanGL/BingHuDJ.aspx";
                break;
            case "22": url = "/Pages/QuShui/13.LaihanGL/XiaoHuDJ.aspx";
                break;
            case "23": url = "/Pages/QuShui/13.LaihanGL/XiaoJinDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "24": url = "/Pages/QuShui/13.LaihanGL/XiaoBiaoDJ.aspx";
                break;
            case "25": url = "/Pages/QuShui/13.LaihanGL/XiaoJinHFDJ.aspx";
                break;
            case "26": url = "/Pages/QuShui/13.LaihanGL/JinGuoHDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "27": url = "/Pages/QuShui/13.LaihanGL/BiaoGuoHDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "28": url = "/Pages/QuShui/13.LaihanGL/FenHuDJ.aspx";
                width = "1100";
                break;
            case "29": url = "/Pages/QuShui/13.LaihanGL/ShenJinLXXGDJ.aspx";
                break;
            case "30": url = "/Pages/QuShui/13.LaihanGL/JinXingZXGDJ.aspx";
                break;
            case "31": url = "/Pages/QuShui/13.LaihanGL/TuiKuanDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "32": url = "/Pages/QuShui/13.LaihanGL/KaiCaiJHDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "33": url = "/Pages/QuShui/13.LaihanGL/CaiGuanJHDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "34": url = "/Pages/QuShui/13.LaihanGL/JiaJiaSLTZDJ.aspx";
                height = "560";
                break;
            case "35": url = "/Pages/QuShui/13.LAIHANGL/XiaoQuShuiKDJ.aspx";
                width = "900";
                height = "560";
                break;
            case "36": url = "/Pages/QuShui/13.LAIHANGL/QuShuiKGHDJ.aspx";
                width = "900";
                height = "560";
                break;
        };
        if (type == 1) {//修改
            url = url + "?laihanbh=" + lhbh + "&id=" + id;
        }
        else {
            url = url;
        }
        layer.open({
            type: 2,
            title:"",
            shadeClose: false,
            shade: 0.8,
            area: [width+"px", height+"px"],
            content: url,
            end: function () {
                load();
            }
        });
    }

    //月度来函快捷登记
    //$("#btnSpeed").click(function () {
    //    window.location = '/Pages/YongShui/13.LAIHANGL/YueDuTZKJDJ.aspx'
    //});
    
    form.on("select(cType)", function () {
        orders = 0;
        load();
    });

    /*event end*/

    function verify() {
        return true;
    }

    

    /*logic end*/
});